﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class UserAddForm : Form
    {
        public UserAddForm()
        {
            InitializeComponent();
        }

        private bool addcheck()
        {
            if (this.textBox1.Text == "")
            {
                MessageBox.Show("请输入用户号码");
                return false;
            }
            if (this.textBox2.Text == "")
            {
                MessageBox.Show("请输入用户名");
                return false;
            }
            if (this.comboBox1.SelectedItem == null)
            {
                MessageBox.Show("请选择性别");
                return false;
            }
            if (this.comboBox2.SelectedItem == null)
            {
                MessageBox.Show("请选择用户权限");
                return false;
            }
            return true;
        }


        private bool userexist()
        {
            int userid = Int32.Parse(this.textBox1.Text);
            MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();

            var query =  from c in dbcontext.tb_user
                         where c.ID == userid
                         select c.ID;

            if (query.Count() != 0)
            {
                MessageBox.Show("添加的用户号码已存在");
                return false;
            }
            return true;
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if ((addcheck() == false) || (userexist() == false))
            {
                return;
            }

            string[] insertdata = new string[11];
            insertdata[0] = this.textBox1.Text;
            insertdata[1] = this.textBox2.Text;
            insertdata[2] = this.comboBox1.Items[this.comboBox1.SelectedIndex].ToString();
            insertdata[3] = this.comboBox2.Items[this.comboBox2.SelectedIndex].ToString();
            insertdata[4] = this.textBox3.Text == "" ? "无" : this.textBox3.Text;
            insertdata[5] = this.dateTimePicker1.Checked == false ? "无" : this.dateTimePicker1.Value.ToString();
            insertdata[6] = this.textBox4.Text == "" ? "无" : this.textBox4.Text;
            insertdata[7] = this.textBox5.Text == "" ? "无" : this.textBox5.Text;
            insertdata[8] = this.textBox6.Text == "" ? "无" : this.textBox6.Text;
            insertdata[9] = this.textBox7.Text == "" ? "无" : this.textBox7.Text;
            insertdata[10] = this.textBox8.Text == "" ? "无" : this.textBox8.Text;

            
  
            MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();
            tb_user item = new tb_user();
            item.Password = "1";
            item.ID = Int32.Parse(this.textBox1.Text);
            item.Name = this.textBox2.Text;
            item.sex = this.comboBox1.Items[this.comboBox1.SelectedIndex].ToString();
            item.Role = this.comboBox2.Items[this.comboBox2.SelectedIndex].ToString();

            if (this.textBox3.Text != "")
            {
                item.IDcard = this.textBox3.Text;
            }

            if (this.dateTimePicker1.Checked)
            {
                item.Birthday = this.dateTimePicker1.Value;
            }

            if (this.textBox4.Text != "")
            {
                item.Phone = this.textBox4.Text;
            }

            if (this.textBox5.Text != "")
            {
                item.email = this.textBox5.Text;
            }

            if (this.textBox6.Text != "")
            {
                item.Workaddress = this.textBox6.Text;
            }

            if (this.textBox7.Text != "")
            {
                item.Homeaddress = this.textBox7.Text;
            }

            if (this.textBox8.Text != "")
            {
                item.Comment = this.textBox8.Text;
            }

            dbcontext.tb_user.InsertOnSubmit(item);

            string detail = UserLog.builddetailstring(UserLog.usermanagerheader, "添加", insertdata, null);

            try
            {
                dbcontext.SubmitChanges();
                UserLog.addusermanagerecord(User.UserID, User.Username, "添加", item.ID, item.Name, "成功", detail);
            }
            
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.ToString());
                UserLog.addusermanagerecord(User.UserID, User.Username, "添加", item.ID, item.Name, "失败", detail);
                return;
            }

            MessageBox.Show("添加用户成功");
        }





        private void UserAddForm_Load(object sender, EventArgs e)
        {
            this.dateTimePicker1.Checked = false;
        }
    }
}
